package com.sztle.admodule.myutils;

import android.app.Activity;
import android.graphics.Color;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.text.style.ForegroundColorSpan;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;

import androidx.annotation.NonNull;

import com.sztle.admodule.IPolicyDialogCallback;
import com.sztle.admodule.R;
import com.sztle.admodule.dialog.BasePrivacyBindingDialog;
import com.sztle.admodule.databinding.DialogPolicyBinding;
import com.sztle.admodule.databinding.DialogRetentionV3Binding;
import com.sztle.admodule.outstatus.AppStatusProcessor;
import com.sztle.admodule.someutils.AppUtils;
import com.sztle.admodule.someutils.ScreenUtils;
import com.sztle.admodule.control.event.SzTrackEvents;
import com.sztle.admodule.control.manager.ControlManager;


public class PolicyDialogHelper {

    public static void showPolicyDialog(Activity activity, IPolicyDialogCallback dialogCallback) {
        BasePrivacyBindingDialog<DialogPolicyBinding> policyDialog = new BasePrivacyBindingDialog<DialogPolicyBinding>(activity) {

            @Override
            public void onAttachedToWindow() {
                super.onAttachedToWindow();
                hideBottomUIMenu();
            }

            @Override
            public DialogPolicyBinding getViewBinding() {
                return DialogPolicyBinding.inflate(getLayoutInflater());
            }

            @Override
            public void initData() {
                dialogBinding.btnAgree.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dismiss();
                        if (dialogCallback != null) {
                            dialogCallback.onSure();
                        }
                        AppStatusProcessor.statusAgreePolicy();
                        ControlManager.reportAppLog(SzTrackEvents.agree_privacy1);
                    }
                });

                dialogBinding.btnCancel.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dismiss();
                        ControlManager.reportAppLog(SzTrackEvents.disagree_privacy1);
                        showRetentionPrivacyDialog(activity,dialogCallback);
                    }

                });


                dialogBinding.tvTitle.setMovementMethod(LinkMovementMethod.getInstance());
                dialogBinding.tvTitle.setFocusable(false);

                String text = "欢迎使用" + AppUtils.getAppName();
                SpannableString spannableTitle = new SpannableString(text);
                spannableTitle.setSpan(new ForegroundColorSpan(Color.parseColor("#66A8FF")), 4, text.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
                dialogBinding.tvTitle.setText(spannableTitle);


                SpannableStringBuilder spannableString = new SpannableStringBuilder(activity.getString(R.string.str_desc_policy));
                spannableString.setSpan(new ClickableSpan() {
                    @Override
                    public void onClick(@NonNull View widget) {
                        if (dialogCallback != null) {
                            dialogCallback.onClickUserPolicy();
                        }
                    }

                    @Override
                    public void updateDrawState(@NonNull TextPaint ds) {
                        ds.setColor(Color.parseColor("#2b43f0"));
                    }
                }, 10, 18, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
                spannableString.setSpan(new ClickableSpan() {
                    @Override
                    public void onClick(@NonNull View widget) {
                        if (dialogCallback != null) {
                            dialogCallback.onClickPrivacyPolicy();
                        }
                    }

                    @Override
                    public void updateDrawState(@NonNull TextPaint ds) {
                        ds.setColor(Color.parseColor("#2b43f0"));
                    }
                }, 19, 25, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
                dialogBinding.tvDesc.setMovementMethod(LinkMovementMethod.getInstance());
                dialogBinding.tvDesc.setHighlightColor(activity.getResources().getColor(R.color.color_transparent));
                dialogBinding.tvDesc.setText(spannableString);
                int height = dialogBinding.getRoot().getMeasuredHeight();

                WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
                layoutParams.width = ScreenUtils.getScreenWidth();
                layoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
                getWindow().setAttributes(layoutParams);
                // getWindow().setLayout(ScreenUtils.getScreenWidth(), height);
            }
        };

        policyDialog.setCanceledOnTouchOutside(false);
        policyDialog.getWindow().setGravity(Gravity.CENTER);

        policyDialog.show();
    }

    public static void showRetentionPrivacyDialog(Activity activity, IPolicyDialogCallback dialogCallback) {
        BasePrivacyBindingDialog<DialogRetentionV3Binding> privacyDialog = new BasePrivacyBindingDialog<DialogRetentionV3Binding>(activity) {

            @Override
            public void onAttachedToWindow() {
                super.onAttachedToWindow();
                hideBottomUIMenu();
            }

            @Override
            public DialogRetentionV3Binding getViewBinding() {
                return DialogRetentionV3Binding.inflate(getLayoutInflater());
            }

            @Override
            public void initData() {
                dialogBinding.btnAgree.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dismiss();
                        AppStatusProcessor.statusAgreePolicy();
                        ControlManager.reportAppLog(SzTrackEvents.agree_privacy2);
                        if (dialogCallback != null) {
                            dialogCallback.onSure();
                        }
                    }
                });

                dialogBinding.btnExit.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dismiss();
                        ControlManager.reportAppLog(SzTrackEvents.disagree_privacy2);
                        if (dialogCallback != null) {
                            dialogCallback.onCancel();
                        }
                    }
                });

                SpannableStringBuilder spannableString = new SpannableStringBuilder(activity.getString(R.string.retention_tips));
                spannableString.setSpan(new ClickableSpan() {
                    @Override
                    public void onClick(@NonNull View widget) {
                        if (dialogCallback != null) {
                            dialogCallback.onClickUserPolicy();
                        }
                    }

                    @Override
                    public void updateDrawState(@NonNull TextPaint ds) {
                        ds.setColor(Color.parseColor("#21C894"));
                    }
                }, 3, 9, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);

                spannableString.setSpan(new ClickableSpan() {
                    @Override
                    public void onClick(@NonNull View widget) {
                        if (dialogCallback != null) {
                            dialogCallback.onClickPrivacyPolicy();
                        }
                    }

                    @Override
                    public void updateDrawState(@NonNull TextPaint ds) {
                        ds.setColor(Color.parseColor("#21C894"));
                    }
                }, 10, 16, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);

                dialogBinding.tvContent.setMovementMethod(LinkMovementMethod.getInstance());
                dialogBinding.tvContent.setHighlightColor(activity.getResources().getColor(R.color.color_transparent));
                dialogBinding.tvContent.setText(spannableString);
            }
        };

        privacyDialog.setCanceledOnTouchOutside(false);
        privacyDialog.getWindow().setGravity(Gravity.CENTER);
        privacyDialog.show();
    }
}
